package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPythonActionEntity;
import com.ruoyi.system.domain.vo.PageVo;
import com.ruoyi.system.mapper.ISysPythonActionMapper;
import com.ruoyi.system.service.IMpSysPythonActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author <a href="https://fengwenyi.com?fs=mpcg">Erwin Feng</a>
 * @since 2024-02-02
 */
@Service
public class MpSysPythonActionServiceImpl implements IMpSysPythonActionService {
    @Autowired
    ISysPythonActionMapper iSysPythonActionMapper;
    @Autowired
    RedisCache redisCache;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public IPage<SysPythonActionEntity> page(SysPythonActionEntity sysPythonActionEntity, PageVo pageVo) {
        IPage<SysPythonActionEntity> sysPythonActionEntityIPage = iSysPythonActionMapper.selectPage(new Page<>(pageVo.getPageNum(),pageVo.getPageSize()),
                new LambdaQueryWrapper<SysPythonActionEntity>()
                        .eq(StringUtils.isNotEmpty(sysPythonActionEntity.getActionName()),SysPythonActionEntity::getActionName,sysPythonActionEntity.getActionName()));
        return sysPythonActionEntityIPage;
    }

    @Override
    public Integer saveAndUpdate(SysPythonActionEntity sysPythonActionEntity) {
        if (sysPythonActionEntity.getId()==null){

            Integer result = iSysPythonActionMapper.insert(sysPythonActionEntity);
            if (result>0){
                //将活动存入缓存
                if (redisCache.getCacheList("action::")!=null) {
                    List<SysPythonActionEntity> sysPythonActionEntities = redisCache.getCacheList("action::");
                    sysPythonActionEntities.add(sysPythonActionEntity);
                    redisTemplate.delete("action::");
                    redisCache.setCacheList("action::", sysPythonActionEntities);
                }else {
                    List<SysPythonActionEntity> sysPythonActionEntities = new ArrayList<>();
                    sysPythonActionEntities.add(sysPythonActionEntity);
                    redisCache.setCacheList("action::", sysPythonActionEntities);
                    System.out.println(redisCache.getCacheList("action::"));
                }

            }
            return result;
        }    Integer result = iSysPythonActionMapper.updateById(sysPythonActionEntity);

        return result;
    }

    //判断活动是否过期
//    @Scheduled(cron = "0 0 9 * * ?")
    public void actionTime(){

    }


}
